package com.zixi.beverage_order.config;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

//    配置拦截
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("getDefaultWebSecurityManager") DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);

        Map<String,String> filterChainDefinitionMap = new LinkedHashMap<>();
        filterChainDefinitionMap.put("/delivery/*","authc");
        filterChainDefinitionMap.put("/pickup/*","authc");
        filterChainDefinitionMap.put("/manage/*","authc");
        filterChainDefinitionMap.put("/system","authc");
        filterChainDefinitionMap.put("/delivery","authc");
        filterChainDefinitionMap.put("/pickup","authc");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        shiroFilterFactoryBean.setLoginUrl("/");//登录界面
        shiroFilterFactoryBean.setUnauthorizedUrl("/unauthorized");//未授权跳转页面

        return shiroFilterFactoryBean;
    }

//    connect Realm
    @Bean(name = "getDefaultWebSecurityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("getUserRealm") UserRealm userRealm){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(userRealm);
        return defaultWebSecurityManager;
    }

//    new Realm
    @Bean(name = "getUserRealm")
    public UserRealm getUserRealm(){return new UserRealm();}
}
